<?php
/**
 * Created by PhpStorm.
 * User: Inhere
 * Date: 15-4-1
 * Time: 上午10:08
 * Used: bootstrap 导航
 * file: Nav.php
 */

namespace ulue\ext\zui;

use ulue\web;
use ulue\core\router;
use ulue\libs\front\Element;

/**
 * @usage: 在视图中 <? $this->loadWidget('Nav',['attrs'=>['data-mark'=>'dfdf']]) ?>
 * 禁用的导航链接 - 在禁用的项目li上添加.disabled
 *
 */
class Nav extends web\Widget
{
    public $wrap = [
        'tag' => 'div',
        'attrs' => [],
    ];

    /**
     * nav-primary -- 主要导航
     * nav-secondary -- 次要导航，样式简单
     * nav-tabs -- 标签页导航
     * nav-pills -- 圆点导航
     *
     ***以上仅能同时设置一个**
     *
     * nav-stacked -- 垂直排列
     * nav-justified -- 自适应宽度,不能与垂直排列同时使用
     */
    public $attrs = [
        'class' => 'nav nav-primary'
    ];

    /**
     * @var array
     */
    public $mainNav = [];

//////////////////////////////// 以上属性设置只针对 mainNav ////////////////////////////////

    /**
     * @var array
     */
    public $customNav   = [];
    /**
     * @var array
     */
    public $customAttrs = [];

    public function __construct()
    {
        parent::init();

        $this->mainNav  = $this->mainNav();
    }

    /**
     * @return mixed
     */
    public function beginExecute()
    {
        $ul = $this->createUlElement($this->mainNav, $this->attrs);

        return $ul->getString();
        // return $this->render('nav');
    }

    /**
     * 主导航配置
     */
    protected function mainNav()
    {
        //  setting
        return [
            /*
            <li class="nav-heading">这是标题</li>
            导航标题(可选)
            */
            'navTitle' => ['text' => '这是标题', 'attrs'=>[] ],
            //'navTitle' => ['img'=>'xxx/logo.png', 'attrs'=>[] ],// 使用图片

            /*
            <li><a href="###"><i class="icon-user"></i> 个人资料 <span class="label label-badge label-success">4</span></a></li>
            // route 当前标签的路由字符串，用于匹配 栏目高亮  e.g. '/post/manage'
            'route'    => [
                'text'    => '',
                'href'    => '' , // 为空时 自动设置 为 javascript:;
                'icon'    => 'icon-home', // 添加图标
                'append'  => '<span class="label label-badge label-success">4</span>',// 在 name 之后追加元素

                // 给 a 链接 添加其他标签属性 e.g. ['title'=>'a title', 'id'=>'ddd', 'data-mark'=>'xxx']
                'attrs' => [],
                'submenu' => [],// ... 设置跟父级类似
            ],
            */

            'index'    => [
                'text'  => '首页',
                'href'  => \Ioc::get('app.uri')->get('index.path') ,
                'icon'  => 'icon-home',
                // 'attrs' => [],
            ],
            'site/index'    => [
                'text'  => '站点',
                'href'  => '' ,
                'icon'  => 'icon-inbox',
                // 'attrs' => [],
                'submenu' => [
                    'site/list' => [
                        'text'  => '列表',
                        'href'  => router\Uri::to('site/list') ,
                        'icon'  => 'icon-list',
                    ],
                    'site/add' => [
                        'text'  => '添加',
                        'href'  => router\Uri::to('site/add') ,
                        'icon'  => 'icon-plus',
                    ],
                ],
            ],
            'site/about'=> ['text' => '关于', 'href' => router\Uri::to('site/about') , 'icon' => 'icon-chat' ],
        ];
    }

    /**
     * @return array
     */
    protected function customNav()
    {
        return [
            // 自定义添加导航 外围元素属性 e.g. ul
            'attrs' => [],
            // 导航菜单配置 跟 mainNav() 的数组一样配置即可
            'config' => []
        ];
    }

    /**
     * @param array $config
     * @param array $attrs
     * @param string $tag
     * @return object Element
     */
    protected function createUlElement(array $config, array $attrs=[], $tag='ul')
    {
        // tag ul
        $ul = new Element($tag,null, $attrs);
        $content = '';

        foreach($config as $route=>$value) {

            // tag li
            // $li = $ul->addChild('li');
            $li = new Element('li');

            // special -- nav Title
            if ( $this->specialHandleTag($li, $route,$value) ) {
                continue;
            }

            if ($route && strstr(\Ioc::get('app.router')->routeString, trim($route,'/ ') ) ) {
                $li->addClass('active');
            }

            // tag a
            $attrs = !empty($value['attrs']) ? $value['attrs'] : [];

            $a = $li->addChild('a', $value['text'], $attrs);

            if (isset($value['href'])) {
                $href = $value['href'];
                $a->setAttr('href', $href ? : 'javascript:;');
            }

            // tag i
            if ( !empty($value['icon']) ) {
                $a->setChildPosition(Element::BEFORE_TEXT)->addChild('i',' ',['class'=>$value['icon'] ]);
            }

            // 有子级元素
            if ( !empty($value['submenu']) ) {
                // class="dropdown-toggle" data-toggle="dropdown"
                $a->addClass('class',"dropdown-toggle")->addAttr('data-toggle',"dropdown");

                $value['append'] = '<span class="caret"></span>';

                $subUl = $this->createUlElement($value['submenu'], ['class'=>"dropdown-menu"],$tag);

                $li->addChild($subUl);
            }

            // custom tag
            if ( !empty($value['append']) ) {
                $append = $value['append'];

                if ($append instanceof Element) {
                    $a->setChildPosition(Element::AFTER_TEXT)->addChild($append);
                } else {
                    $a->addContent((string)$append);
                }
            }

            $content .= (string)$li;
        }// end foreach

        return $ul->setContent($content);
    }

    /**
     * @param Element $tag
     * @param $route
     * @param $value
     * @return bool
     */
    protected function specialHandleTag(Element $tag, $route, $value)
    {
        // special -- nav Title
        if ( trim($route)==='navTitle' ) {
            $tag->setClass('nav-heading');

            if (isset($value['img'])) {
                $tag->addChild('img')->setAttr('src', $value['img']);
            } else {
                $name = isset($value['text']) ? $value['text']: 'This Title';
                $tag->setContent($name);
            }

            return true;
        }

        return false;
    }

}// end class Nav
